package com.ihome.excel.xlsx.engine.imports;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import com.ihome.excel.xlsx.entity.ValidateRule;
import com.ihome.excel.xlsx.exceptions.ThrowableDefine;
import com.ihome.excel.xlsx.tool.RegexUtils;

@SuppressWarnings("all")
public class CellParseEngine {
    
    private static RegexUtils utils = new RegexUtils();

	public Object getCellValue(Cell cell, ValidateRule validateRule) {
		if (cell == null) {
		    throw ThrowableDefine.PARAMETER_VALIDATE_EXP.getIllegaArgumentExp("cell");			
		}
		if(validateRule == null){
			int cellType = cell.getCellType();
			if(cellType == Cell.CELL_TYPE_NUMERIC) {
				return cell.getNumericCellValue();
			}else if(cellType == Cell.CELL_TYPE_BOOLEAN) {
				return cell.getBooleanCellValue();
			}else if(cellType == cell.CELL_TYPE_FORMULA) {
				return cell.getCellFormula();
			}else {
				return cell.getStringCellValue();
			}
		}
		Object object = null;		
		switch (validateRule.getDataType().getCode()) {
		    /**
		     * INT类型
		     */
			case 1:
			    if (validateRule.isNotNull()) {   
			        String strValue = cell.getStringCellValue();
                    if (strValue == null || "".equals(strValue)) {
                        throw ThrowableDefine.CELL_VALUE_NOT_EMPTY.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
                    }                                   
                }
				if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {		
					object = new Double(cell.getNumericCellValue()).intValue();
				} else {
				    throw ThrowableDefine.CELL_VALUE_TYPE_NOT_MISMATCH.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1), String.valueOf(validateRule.getDataType()),cell.getCellType());
				}
				break;
			/**
			 * DOUBLE类型
			 */
			case 2:
			    if (validateRule.isNotNull()) {   
                    String strValue = cell.getStringCellValue();
                    if (strValue == null || "".equals(strValue)) {
                        throw ThrowableDefine.CELL_VALUE_NOT_EMPTY.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
                    }                                   
                }
				if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
					object = cell.getNumericCellValue();
				} else {
				    throw ThrowableDefine.CELL_VALUE_TYPE_NOT_MISMATCH.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1), String.valueOf(validateRule.getDataType()),cell.getCellType());
				}
				break;
			/**
			 * LONG类型
			 */
			case 3:
			    if (validateRule.isNotNull()) {   
                    String strValue = cell.getStringCellValue();
                    if (strValue == null || "".equals(strValue)) {
                        throw ThrowableDefine.CELL_VALUE_NOT_EMPTY.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
                    }                                   
                }
				if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
					object = Math.round(cell.getNumericCellValue());
				} else {
				    throw ThrowableDefine.CELL_VALUE_TYPE_NOT_MISMATCH.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1), String.valueOf(validateRule.getDataType()),cell.getCellType());
				}
				break;
			/**
			 * STRING类型
			 */
			case 4:
			    String strValue = cell.getStringCellValue();
				if (validateRule.isNotNull()) {					
					if (strValue == null || "".equals(strValue)) {
						throw ThrowableDefine.CELL_VALUE_NOT_EMPTY.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
					}									
				}
				if(validateRule.isEmail()){
				    if(!utils.isEmail(strValue)){
				        throw ThrowableDefine.CELL_VALUE_NOT_EMAIL.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
				    }
				}
				if(validateRule.isIdCard()){
				    if(!utils.isIdCard(strValue)){
				        throw ThrowableDefine.CELL_VALUE_NOT_IDCARD.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
				    }
				}
				if(validateRule.isMobile()){
				    if(!utils.isMobile(strValue)){
				        throw ThrowableDefine.CELL_VALUE_NOT_MOBILE.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
				    }
				}
				if(validateRule.isSeatPhone()){
				    if(!utils.isPhone(strValue)){
				        throw ThrowableDefine.CELL_VALUE_NOT_PHONE.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
				    }
				}
				if(validateRule.getStrLengthGt() > 0){
                    if(strValue.length() < validateRule.getStrLengthGt()){
                        throw ThrowableDefine.CELL_VALUE_LENGTH_GT.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1),validateRule.getStrLengthGt());
                    }
                }
                if(validateRule.getStrLengthLt() > 0){
                    if(strValue.length() > validateRule.getStrLengthLt()){
                        throw ThrowableDefine.CELL_VALUE_LENGTH_LT.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1),validateRule.getStrLengthLt());
                    }
                }  
                if(validateRule.getStrLengthEquals() > 0){
                    if(strValue.length() != validateRule.getStrLengthEquals()){
                        throw ThrowableDefine.CELL_VALUE_LENGTH_LT.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1),validateRule.getStrLengthEquals());
                    }
                }
				object = strValue;
				break;
			/**
			 * DATE类型
			 */
			case 5:
			    if (validateRule.isNotNull()) {   
                    String cellValue = cell.getStringCellValue();
                    if (cellValue == null || "".equals(cellValue)) {
                        throw ThrowableDefine.CELL_VALUE_NOT_EMPTY.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
                    }                                   
                }
				if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC && HSSFDateUtil.isCellDateFormatted(cell)) {
				    Date value = cell.getDateCellValue();
                    object = value;
				} else {
				    String value = cell.getStringCellValue();                      
                    DateFormat format = new SimpleDateFormat("yyyyMMdd");
                    try {
                        object = format.parse(value);
                    } catch (ParseException e) {
                        throw ThrowableDefine.CELL_VALUE_TYPE_NOT_MISMATCH.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1), String.valueOf(validateRule.getDataType()),cell.getCellType());
                    }
				}
				break;
			/**
			 * BIGDECIMAL类型
			 */
			case 6:
			    if (validateRule.isNotNull()) {   
                    String cellValue = cell.getStringCellValue();
                    if (cellValue == null || "".equals(cellValue)) {
                        throw ThrowableDefine.CELL_VALUE_NOT_EMPTY.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1));
                    }                                   
                }
				if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
					double value = cell.getNumericCellValue();
					BigDecimal bigDecimal = null;
					if(validateRule.getBigDecimalScale() > 0){
					    bigDecimal = new BigDecimal(value).setScale(validateRule.getBigDecimalScale(), validateRule.getBigDecimalRoundMode());
					}else{
					    bigDecimal = new BigDecimal(value);
					}
					object = bigDecimal;
				} else {
				    throw ThrowableDefine.CELL_VALUE_TYPE_NOT_MISMATCH.getCellValidateExp((cell.getRowIndex() + 1),(cell.getColumnIndex() + 1), String.valueOf(validateRule.getDataType()),cell.getCellType());
				}
				break;
			default:
				throw ThrowableDefine.CELL_VALUE_UNRECOGNIZED.getCellValidateExp(String.valueOf(validateRule.getDataType()));
		}
		return object;
	}
}
